
class Solution:
    def minimumOperations(self, leaves: str) -> int:
        count=0
        if leaves[0]=='y':
            count+=1
        if leaves[-1]=="y":
            count+=1
        lindex=0
        for lindex in range(1,len(leaves)):
            if leaves[lindex]=='y':
                break
        rindex=len(leaves)-2
        while leaves[rindex]=='r':
            rindex-=1
        if rindex==lindex:
                return count
        rorder=[]
        yorder=[]
        while lindex<=rindex:
            rc=0
            yc=0
            for i in range(lindex,rindex+1):
                if leaves[i]=='r':
                    break
                yc+=1
            yorder.append(yc)
            for j in range(i,rindex+1):
                if leaves[j]=='y':
                    break
                rc+=1
            if rc==0:
                break
            rorder.append(rc)
            lindex=j
        if len(yorder)==0:
            return count+1
        center=yorder.index(max(yorder))
        left=center-1
        print(yorder)
        print(rorder)
        while left>0:
            if yorder[left]<rorder[left]:
                yorder[left-1]+=(yorder[left]+rorder[left])
            else:
                rorder[left-1]+=(yorder[left]+rorder[left])
            left-=1
        if yorder[0]>rorder[0]:
            count+=rorder[0]
        else:
            count+=yorder[0]
        right=0
        yorder=yorder[center+1:]
        rorder=rorder[center:]
        while right<len(yorder)-1:
            if yorder[right]<rorder[right]:
                yorder[right+1]+=(yorder[right]+rorder[right])
                count+=yorder[right]
            else:
                rorder[right+1]+=(yorder[right]+rorder[right])
                count+=rorder[right  nm  ]
            right+=1
        print(yorder) 
        print(rorder) 
        return count

s=Solution()
print(s.minimumOperations("ryyryyyrryyyyyryyyrrryyyryryyyyryyrrryryyyryrryrrrryyyrrrrryryyrrrrryyyryyryrryryyryyyyryyrryrryryy")